package com.zhongen.oa.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.zhongen.admin.api.entity.SysUser;
import com.zhongen.common.security.service.PigUser;
import com.zhongen.common.security.util.SecurityUtils;
import com.zhongen.oa.entity.ProjectBasicInfoEntity;
import com.zhongen.oa.entity.ProjectMainTaskEntity;
import com.zhongen.oa.entity.ProjectRequirementEntity;
import com.zhongen.oa.entity.vo.ProjectMainTaskVo;
import com.zhongen.oa.mapper.ProjectMainTaskMapper;
import com.zhongen.oa.service.ProjectMainTaskService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 主线任务
 *
 * @author Code Generator
 * @date 2024-01-12 10:05:52
 */
@Service
public class ProjectMainTaskServiceImpl extends ServiceImpl<ProjectMainTaskMapper, ProjectMainTaskEntity>
		implements ProjectMainTaskService {

	/**
	 * @param page
	 * @param projectMainTask
	 * @return
	 */
	@Override
	public IPage<ProjectMainTaskVo> getProjectMainTaskPage(Page page, ProjectMainTaskEntity projectMainTask) {
		MPJLambdaWrapper<ProjectMainTaskEntity> wrapper = new MPJLambdaWrapper<ProjectMainTaskEntity>();
		wrapper.selectAll(ProjectMainTaskEntity.class)
			.select(SysUser::getName)
			.leftJoin(SysUser.class, SysUser::getUserId, ProjectMainTaskEntity::getCommander);
		wrapper.select(ProjectBasicInfoEntity::getProjectName)
			.leftJoin(ProjectBasicInfoEntity.class, ProjectBasicInfoEntity::getId, ProjectMainTaskEntity::getFkId);
		wrapper.select(ProjectRequirementEntity::getRequirementName)
			.leftJoin(ProjectRequirementEntity.class, ProjectRequirementEntity::getId, ProjectMainTaskEntity::getFkId);
		wrapper.like(Objects.nonNull(projectMainTask.getTaskName()), ProjectMainTaskEntity::getTaskName,
				projectMainTask.getTaskName());
		wrapper.eq(Objects.nonNull(projectMainTask.getTaskStatus()), ProjectMainTaskEntity::getTaskStatus,
				projectMainTask.getTaskStatus());
		return baseMapper.selectJoinPage(page, ProjectMainTaskVo.class, wrapper);
	}

	@Override
	public List<ProjectMainTaskVo> exportList(ProjectMainTaskEntity projectMainTask, Long[] ids) {
		MPJLambdaWrapper<ProjectMainTaskEntity> wrapper = new MPJLambdaWrapper<ProjectMainTaskEntity>();
		wrapper.selectAll(ProjectMainTaskEntity.class)
			.select(SysUser::getName)
			.leftJoin(SysUser.class, SysUser::getUserId, ProjectMainTaskEntity::getCommander);
		wrapper.select(ProjectBasicInfoEntity::getProjectName)
			.leftJoin(ProjectBasicInfoEntity.class, ProjectBasicInfoEntity::getId, ProjectMainTaskEntity::getFkId);
		wrapper.select(ProjectRequirementEntity::getRequirementName)
			.leftJoin(ProjectRequirementEntity.class, ProjectRequirementEntity::getId, ProjectMainTaskEntity::getFkId);
		wrapper.like(Objects.nonNull(projectMainTask.getTaskName()), ProjectMainTaskEntity::getTaskName,
				projectMainTask.getTaskName());
		wrapper.eq(Objects.nonNull(projectMainTask.getTaskStatus()), ProjectMainTaskEntity::getTaskStatus,
				projectMainTask.getTaskStatus());
		List<ProjectMainTaskVo> projectMainTaskVos = baseMapper.selectJoinList(ProjectMainTaskVo.class, wrapper);
		for (ProjectMainTaskVo projectMainTaskVo : projectMainTaskVos) {
			String taskStatus = projectMainTaskVo.getTaskStatus();
			/**
			 *
			 * when 'wait' then '未开始' when 'doing' then '进行中' when 'suspend' then '已挂起'
			 * when 'closed' then '已关闭'
			 */
			switch (taskStatus) {
				case "wait":
					projectMainTaskVo.setTaskStatus("未开始");
					break;
				case "doing":
					projectMainTaskVo.setTaskStatus("进行中");
					break;
				case "suspend":
					projectMainTaskVo.setTaskStatus("已挂起");
					break;
				case "closed":
					projectMainTaskVo.setTaskStatus("已关闭");
					break;
				default:
					projectMainTaskVo.setTaskStatus("");
					break;
			}
		}
		return projectMainTaskVos;
	}

	@Override
	public IPage<ProjectMainTaskVo> getProjectMainTaskPageApp(Page page, ProjectMainTaskEntity projectMainTask) {
		if(projectMainTask.getCommander()!=null){
			PigUser loginUser = SecurityUtils.getUser();
			projectMainTask.setCommander(loginUser.getId());
		}
		return baseMapper.selectPageApp(page, projectMainTask);
	}

}
